package com.cf.common.rx;


import android.annotation.SuppressLint;
import android.util.Log;

import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.OnLifecycleEvent;

import com.trello.rxlifecycle2.android.ActivityEvent;

import io.reactivex.subjects.BehaviorSubject;

/**
 * @author yangji
 */
public class LifecycleRegistryToBehaviorSubject implements LifecycleObserver, RxEventSubject {
    private static final String TAG = "LifecycleRegistryToBehaviorSubject";

    private final BehaviorSubject<ActivityEvent> lifecycleSubject = BehaviorSubject.create();

    public static LifecycleRegistryToBehaviorSubject create() {
        return new LifecycleRegistryToBehaviorSubject();
    }

    @SuppressLint("LongLogTag")
    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreate() {
        Log.e(TAG, "onCreate");
        lifecycleSubject.onNext(ActivityEvent.CREATE);
    }

    @SuppressLint("LongLogTag")
    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart() {
        Log.e(TAG, "onStart");
        lifecycleSubject.onNext(ActivityEvent.START);
    }

    @SuppressLint("LongLogTag")
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume() {
        Log.e(TAG, "onResume");
        lifecycleSubject.onNext(ActivityEvent.RESUME);
    }

    @SuppressLint("LongLogTag")
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause() {
        Log.e(TAG, "onPause");
        lifecycleSubject.onNext(ActivityEvent.PAUSE);
    }

    @SuppressLint("LongLogTag")
    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop() {
        Log.e(TAG, "onStop");
        lifecycleSubject.onNext(ActivityEvent.STOP);
    }

    @SuppressLint("LongLogTag")
    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    public void onDestroy() {
        Log.e(TAG, "onDestroy");
        lifecycleSubject.onNext(ActivityEvent.DESTROY);
    }

    @Override
    public BehaviorSubject<ActivityEvent> getSubject() {
        return lifecycleSubject;
    }
}